#include  <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/printk.h>
#include <linux/init.h>
#include <linux/ide.h>
#define CHADEV_MAJOR   200//主设备号
#define CHADeV_NAME  "XYTCHARdev"  //
static char readbuff[100];//读缓冲
static char writebuff[100];//写缓冲
static char kernelbuff[]="kernel date!";

static 	int chrdevbase_open (struct inode *inode, struct file *filp)
{
    printk("Open succes \r\n");
    return 0;
}
static int  chrdevbase_release(struct inode *inode , struct file *filp)
{
    printk("relase succes \r\n");
    return 0;
}

static ssize_t chrdevbase_read (struct file * fs, char __user * buff, size_t count, loff_t  * opps)
{
    int ret = 0;
    //printk(KERN_EMERG"read succes \r\n");
    memcpy(readbuff,kernelbuff,sizeof(kernelbuff));
    ret = copy_to_user(buff,readbuff,count);
    if(ret == 0)
    {

    }else 
    {

    }
    return 0;
}
static ssize_t chrdevbase_write(struct file *fs, const char __user *buff, size_t count, loff_t *opps)
{
    int ret =0;
    //printk(KERN_EMERG"write  succes \r\n");
    ret = copy_from_user(writebuff,buff,count);
    if(ret == 0)
    {
        printk("kernel recie: %s \r\n",writebuff);     
    }else 
    {
        printk("wirte errror;  %s \r\n",writebuff);     
    }
    return 0; 
}
static struct  file_operations Chrdevfos  = 
{
	.owner = THIS_MODULE,	
	.open = chrdevbase_open,
	.read = chrdevbase_read,
	.write = chrdevbase_write,
	.release = chrdevbase_release,
};




static int __init Chrdev_init(void)
{
    int ok =0;
    printk(" XYT moude  init\r\n");
    //注册字符设备
    ok =  register_chrdev(CHADEV_MAJOR, CHADeV_NAME,&Chrdevfos);
    if(ok  <0)
    {
        printk(" char zuche fail\r\n");
    }
    else 
    {
         printk(" char zuche succes\r\n");
    }

    return 0;
}
static void __exit Chrdev_exit(void)
{
     printk(" XYT moude  exit \r\n");
     unregister_chrdev(CHADEV_MAJOR, CHADeV_NAME);
}

module_init(Chrdev_init);
module_exit(Chrdev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xyt");